﻿
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include "font.h"
#include <sys/ioctl.h>

#define SET_DISP_LOC  0
#define WRITE_LEN_DATAS   1
static int fd;

static void oled_disp_char(unsigned char x, unsigned char y, unsigned char c)
{
	unsigned char loc_buff[2];
	unsigned char *dots = oled_asc2_8x16[c - ' '];
	/*显示字符下半部分*/
	loc_buff[0] = x;
	loc_buff[1] = y;
	ioctl(fd, SET_DISP_LOC, loc_buff);
	ioctl(fd, WRITE_LEN_DATAS | 8<<8, dots);
	/*显示字符下半部分*/
	loc_buff[0] = x;
	loc_buff[1] = y + 1;
	ioctl(fd, SET_DISP_LOC, loc_buff);
	ioctl(fd, WRITE_LEN_DATAS | 8<<8, &dots[8]);
}

static void oled_disp_string(unsigned char x, unsigned char y, char *str)
{
	int i = 0;
	while(str[i])
	{
		oled_disp_char(x, y, str[i++]);
		x += 8;
		if (x > 127)
		{
			x = 0;
			y += 2;
		}		
	}
}


static void oled_disp_chinese(unsigned char x, unsigned char y, char no)
{	

	int i;
	unsigned char loc_buff[2];

	/*显示字符上半部分*/
	loc_buff[0] = x;
	loc_buff[1] = y;
	ioctl(fd, SET_DISP_LOC, loc_buff);
	for (i = 0; i < 16; i++)
	{
		ioctl(fd, WRITE_LEN_DATAS | (1<<8), &hz_1616[no][2*i]);
	
	}
	/*显示字符下半部分*/
	loc_buff[0] = x;
	loc_buff[1] = y + 1;
	ioctl(fd, SET_DISP_LOC, loc_buff);
	for (i = 0; i < 16; i++)
	{
		ioctl(fd, WRITE_LEN_DATAS | (1<<8), &hz_1616[no][2*i + 1]);
	
	}

}



static void oled_disp_test(void)
{
	int i;
	oled_disp_string(20, 0, "ZMQZHM");
	oled_disp_string(0, 2, "book.100ask.net");
	for (i = 0; i < 3; i++)
		oled_disp_chinese(i*32, 4, i);
}



/*
 * ./oled_zmq_test /dev/oled_zmq  
 */

int main(int argc, char **argv)
{

	int ret;
	unsigned long data[2];


	if (argc != 2)
	{
		printf("Usage: %s /dev/oled_zmq \n", argv[0]);
		return -1;
	}
	
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("Open %s err.\n", argv[1]);
		return -1;
	}

	oled_disp_test();

	close(fd);
	return 0;
}


